import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)



const routes = [
        {
                path: '/detail/:id',
                name: 'detail',
                meta: { title: '详情' },
                component: () => import('../views/Detail.vue')
        },
        {
                path: '/home',
                name: 'home',
                meta: { title: 'home' },
                component: () => import('../views/Home.vue'),
                children: [
                        {
                                path: '/home/baotuan',
                                name: 'baotuan',
                                meta: { title: '爆爆团' },
                                component: () => import('../views/home/BaoTuan.vue')
                        },
                        {
                                path: '/home/dingdan',
                                name: 'dingdan',
                                meta: { title: '订单' },
                                component: () => import('../views/home/DingDan.vue')
                        },
                        {
                                path: '/home/shouye',
                                name: 'shouye',
                                meta: { title: '首页' },
                                component: () => import('../views/home/ShouYe.vue')
                        },
                        {
                                path: '/home/wode',
                                name: 'wode',
                                meta: { title: '我的' },
                                component: () => import('../views/home/WoDe.vue')
                        },
                ]
        },
        {
                path: '/mine',
                name: 'mine',
                meta: { title: '页面2' },
                component: () => import('../views/Mine.vue'),

        },
        {
                path: '/order',
                name: 'order',
                meta: { title: '页面3' },
                component: () => import('../views/Order.vue')
        },
        {
                path: '/zhuce',
                name: 'zhuce',
                meta: { title: '注册' },
                component: () => import('../views/ZhuCe.vue')
        },
        {
                path: '/denglu',
                name: 'denglu',
                meta: { title: '登录' },
                component: () => import('../views/DengLu.vue')
        },
        {
                path: '/shoucang',
                name: 'shoucang',
                meta: { title: '收藏' },
                component: () => import('../views/ShouCang.vue')
        },
        {
                path: '/',
                redirect: '/home/shouye'
        },
        {
                path: '*',
                meta: { title: '错误啦' },
                component: () => import('../views/NotFound.vue')
        }
]


const router = new VueRouter({
        mode: 'history',
        base: process.env.BASE_URL,
        routes
})

router.beforeEach((to, from, next) => {
        document.title = to.meta.title;
        var token = localStorage.getItem('token');
        if (to.path == '/home/dingdan' || to.path == '/home/wode') {
                if (token) {
                        next()
                } else {
                        next('/denglu')
                }
        } else {
                next()
        }

        //对于后台管理系统这种应用, 只有登录页是可以随意访问的, 但是其他所有页面都是必须在登陆以后才可以访问
        // if (to.path == '/zhuce') { //跳往 注册页 , 允许跳转
        //         next();
        // } else {
        //         if (to.path == '/denglu') { //跳往 登录页
        //                 if (token) { //已登录 , 重定向到 "/"
        //                         next('/');
        //                 } else { //未登录 , 允许跳往 登录页
        //                         next();
        //                 }
        //                 // next();
        //         } else { //跳往 非登录页
        //                 if (token) { //已登录 , 允许访问
        //                         next();
        //                 } else { //未登录 , 重定向到 "/login"
        //                         next('/denglu');
        //                 }
        //         }
        // }

})

export default router
